package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;

import com.sky.dto.*;
import com.sky.pojo.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;


/**
 * 员工管理
 * 请求层
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 新增员工信息
     *
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result add(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.add(employeeDTO);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<Object> get(EmployeePageQueryDTO pagedto) {
        log.info("分页查询名字{}，第一页{}，分页{}", pagedto.getName(),
                pagedto.getPage(), pagedto.getPageSize());
        PageResult page = employeeService.get(pagedto);

        return Result.success(page);
    }

    /**
     * 员工修改密码
     *
     * @param passwordDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("员工修改密码")
    public Result xiuGai(@RequestBody PasswordEditDTO passwordDTO) {
        String oldPassword = passwordDTO.getOldPassword();
        log.info("请求员工信息{},旧密码{},新密码{}", passwordDTO, oldPassword, passwordDTO.getNewPassword());

        employeeService.xiugai(passwordDTO);

        return Result.success();
    }


    /**
     * 启动、禁用员工账号
     * @param
     * @param
     * @return
     */
    @ApiOperation("启动、禁用员工账号")
    @PostMapping("/status/{status}")//@PathVariable:从路径中提取stats值  @RequestParam：通过 URL 的查询字符串传递
    public Result OffNo(@PathVariable("status")int status,@RequestParam("id") Long id) {
        log.info("请求状态{},员工id{}", status,id);
        employeeService.offNo(status,id);

        return Result.success();
    }


    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询员工信息")
    @GetMapping("/{id}")
    public Result<Object> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }


    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        //                                      员工id
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }


}
